package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.cust.IVerifyCustService;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CuccCustInfoDTO;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 客户管理员联系电话
 */
public class AdminContactAttrHandler extends AbstractAttrHandler {

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        String sceneInstId = data.getSceneInstId();
        String custId = data.getCustId();
        Map<String, Object> extParameter = querySceneInstService.getExtParameter(sceneInstId);
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId, custId);
        String cateId = CuccPosServiceUtils.getCateId(data.getSceneInstId(), data.getCustId());
        String serviceOfferId = sceneInst.getServiceOfferId();
        // “行业5G消息”产品
        if (KiteStringUtils.equals(cateId, "2000966") && Objects.nonNull(extParameter)
            && !extParameter.containsKey("VERIFY_CUST_RSP") && KiteStringUtils.equals(serviceOfferId, ServiceOfferConsts.SERVICE_OFFER_ORDER)) {
            CuccCustInfoDTO cuccCustInfoDTO = CuccPosServiceUtils.queryNewCustInfo(data.getCustId());
            String custName = cuccCustInfoDTO.getCustName();
            String provinceCode = cuccCustInfoDTO.getProvinceCode();
            //名单制客户校验接口
            IVerifyCustService verifyCustService = SpringUtil.getBean(IVerifyCustService.class);
            Map<String, Object> verifyCustMap = verifyCustService.qryCustAttributes(custName, provinceCode);
            if (KiteMapUtils.isNotEmpty(verifyCustMap) && verifyCustMap.containsKey("MANAGER_PHONE")){
                String managerPhone = (String) verifyCustMap.get("MANAGER_PHONE");
                //“客户管理员联系电话”默认值由5G消息业务平台提供，但是可以手动更改
                if (KiteStringUtils.isNotEmpty(managerPhone)) {
                    attr.setDefaultValue(managerPhone);
                    attr.setValue(managerPhone);
                    operateSceneInstService.setAttr(sceneInstId,inst.getInstType(),inst.getInstId(),attr);
                }
                String respDesc = (String) verifyCustMap.get("RESP_DESC");
                String respCode = (String) verifyCustMap.get("RESP_CODE");
                Map<String, Object> map = new HashMap<>();
                map.put("RESP_DESC", respDesc);
                map.put("RESP_CODE", respCode);
                operateSceneInstService.putExtParameter(sceneInstId, "VERIFY_CUST_RSP", map);
            }
        }
    }
}
